package cn.zlj.pethome.basic.interceptor;

import cn.zlj.pethome.basic.annotation.PreAuthorize;
import cn.zlj.pethome.basic.domain.Logininfo;
import cn.zlj.pethome.basic.jwt.JwtUtils;
import cn.zlj.pethome.basic.jwt.Payload;
import cn.zlj.pethome.basic.jwt.RsaUtils;
import cn.zlj.pethome.basic.jwt.UserInfo;
import cn.zlj.pethome.system.mapper.PermissionMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class Interceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断有没有token
        String token = request.getHeader("token");
        if(StringUtils.isNotBlank(token)){
            //判断token中有没数据
           // Logininfo logininfo =(Logininfo) redisTemplate.opsForValue().get(token);
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("jwt/pethome_auth_rsa.pub").getFile());
            Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
            UserInfo userInfo = payload.getUserInfo();
            if(userInfo.getLogininfo()!=null){
                //如果是后台员工才判断
                if(userInfo.getLogininfo().getType()==0){
                  //获取当前请求的方法
                    HandlerMethod method= (HandlerMethod) handler;
                    //获取当前方法注解
                    PreAuthorize annotation = method.getMethodAnnotation(PreAuthorize.class);
                    if (annotation!=null){
                        //获取注解上的sn
                        String sn = annotation.sn();
                        //查询当前员工的权限路径
                        //List<String> url=permissionMapper.findByLoginInfo((userInfo.getLogininfo().getId()));
                        List<String> url = userInfo.getPermissions();
                        //判断当前路径是否在url中
                        if(!url.contains(sn)){
                            // 3.1.设置编码字符集
                            response.setCharacterEncoding("UTF-8");
                            // 3.2.设置响应参数类型为JSON
                            response.setContentType("application/json;charset=UTF-8");
                            // 3.3.获取到打印流,并设置响应数据
                            PrintWriter writer = response.getWriter();
                            writer.write("{\"success\":false,\"resultObj\":\"noPermission\"}");
                            // 3.4.刷新、关流
                            writer.flush();
                            writer.close();
                            return false;
                        }

                    }

                }
                //重置redis时间
                //redisTemplate.expire(token,30, TimeUnit.MINUTES);
                return true;
            }
        }
        // 为空 我们要返回 {"success":false,"resultObj":"nologin"}
        // 3.1.设置编码字符集
        response.setCharacterEncoding("UTF-8");
        // 3.2.设置响应参数类型为JSON
        response.setContentType("application/json;charset=UTF-8");
        // 3.3.获取到打印流,并设置响应数据
        PrintWriter writer = response.getWriter();
        writer.write("{\"success\":false,\"resultObj\":\"noLogin\"}");
        // 3.4.刷新、关流
        writer.flush();
        writer.close();
        return false;
    }
}
